import crypto from 'crypto';

const IDChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F', 'G',
    'H', 'I', 'J', 'K', 'L', 'M', 'N',
    'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z'];

export class TokenUtils {

    public static genWorldServerLoginToken(uid: string, worldServerUrl: string, subWorldId: string, time: number) {
        const SECRET_KEY = 'do not forget to modify here when start your own project';
        let content = uid + worldServerUrl + subWorldId + time + SECRET_KEY;
        let token = crypto.createHash('md5').update(content).digest('hex');
        return token;
    }

    public static genID(len: number, numOnly?: boolean): string {
        let id = '';
        let charNum = numOnly ? 10 : IDChars.length;
        for (let i = 0; i < len; ++i) {
            id += IDChars[Math.floor(Math.random() * charNum)];
        }
        return id;
    }
}